﻿using Labthink.PCSoft.UniversalHost4.ControlInterface;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class TileMenu
    {
        public ObservableCollection<TileMenuItem> InstrumentMenus { get; set; } = new ObservableCollection<TileMenuItem>();

        public ObservableCollection<TileMenuItem> SystemMenus { get; set; } = new ObservableCollection<TileMenuItem>();

        public Control ParentControl { get; set; }

        public TileMenu()
        {
            InitializeComponent();
        }

        private void TileMenu_OnLoaded(object sender, RoutedEventArgs e)
        {
            TileMenuItem mi = new TileMenuItem
            {
                Title = LanguageManager.GetLanguageValue("WinTitle_Exit"),
                WindowType = null
            };

            SystemMenus.Add(mi);
            lbMenu1.ItemsSource = InstrumentMenus;
            lbMenu2.ItemsSource = SystemMenus;

            if (!InstrumentMenus.Any())
            {
                lblIns.Visibility = Visibility.Collapsed;
                lbMenu1.Visibility = Visibility.Collapsed;
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Hide();
        }

        private void btnItem_OnClick(object sender, RoutedEventArgs e)
        {
            Hide();
            var wt = (Type)((Button)sender).CommandParameter;
            if (wt != null)
            {
                Window w = (Window)Activator.CreateInstance(wt);
                w.Show();
            }
            else
            {
                var c = ParentControl as IPart;
                c.RaiseLogoutEvent();
            }
        }
    }
}
